package com.team2.outpatient_sys.dao;

import com.team2.outpatient_sys.pojo.Distribution;
import com.team2.outpatient_sys.pojo.ShowDistribution;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DistributionDao {
    @Insert("insert into distribution values(null,#{medicine},#{distributionName},#{receiveName},#{distributionNumber},#{distributionTime},#{distributionState})")
    public Integer add(Distribution distribution);

    @Delete("delete from distribution where id=#{id}")
    public Integer delete(Integer id);

    @Update("update distribution set receive_name=#{receiveName}")
    public Integer update(Distribution distribution);

    @Results({
            @Result(column = "medicine_id" ,property = "medicine",
                    one = @One (
                            select = "com.team2.outpatient_sys.dao.MedicineDao.findAllById")
            )
    })
    @Select("select * from distribution where distribution_state = '否'")
    public List<ShowDistribution> findAll();

    @Select("select * from distribution where id=#{id}")
    public Distribution findById(Integer id);

    @Update("update distribution set distribution_state='是',receive_name=#{name} where medicine_id=#{medicineId} and distribution_state = '否'")
    void updateStateAndName(@Param("medicineId") Integer medicineId, @Param("name") String name);

    @Results({
            @Result(column = "medicine_id" ,property = "medicine",
                    one = @One (
                            select = "com.team2.outpatient_sys.dao.MedicineDao.findAllById")
            )
    })
    @Select("select * from distribution where distribution_state = '是'")
    List<ShowDistribution> findAllByDistributionState();

}
